//
//  commViewController.m
//  YiXiu
//
//  Created by ZHY on 16/6/26.
//  Copyright © 2016年 zzz. All rights reserved.
//

#import "commViewController.h"
#import "AppDelegate.h"
#import "ScrollerVC.h"
#import "pureTextViewController.h"
#import "pureImageViewController.h"
#import "voiceViewController.h"
#import "UserViewController.h"
#import "AutoSizeTableViewCell.h"
#import "TextUserController.h"
#import "CommModel.h"
#import "NSObject+JSONCategories.h"

@interface commViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * cellModels;
@property (nonatomic, assign) NSInteger  index;


@end

@implementation commViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.cellModels = [NSMutableArray array];
    self.index = 1;
    [self getData];
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    
    NSLog(@"%s",__func__);
}




- (void)getData{
    /*
     dang	Int	第几页
     p_type	String	帖子类型
     */
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"dang"] = [NSString stringWithFormat:@"%ld",self.index];

    [NetWorkingTool POST:@"post/getPostList" parameters:dic success:^(id responseObject, NSError *error) {
        NSLog(@"------zzz----%@",[responseObject mj_JSONObject]);
        
        NSDictionary *dict = [responseObject mj_JSONObject];
        NSArray *dataArr = dict[@"json2"];
        NSArray *dataArr2 = dict[@"json1"];

        for (NSDictionary *dic in dataArr2) {
            CommModel *model = [CommModel mj_objectWithKeyValues:dic];
            AutoSizeModel *model1 =  [AutoSizeModel tranfromByCommModel:model];
            [self.cellModels addObject:model1];
        }
        
        for (NSDictionary *dic in dataArr) {
            CommModel *model = [CommModel mj_objectWithKeyValues:dic];
            AutoSizeModel *model1 =  [AutoSizeModel tranfromByCommModel:model];
            [self.cellModels addObject:model1];
        }
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        self.index++;
    } failure:^(id responseObject, NSError *error) {
        NSLog(@"error : %@",error);
        
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        [self.tableView.mj_header endRefreshingWithCompletionBlock:nil];
        
    }];
    
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WidthOfView([UIScreen mainScreen]), HeightOfView([UIScreen mainScreen]) - 108) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        //下拉上拉刷新
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self getData];
        }];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self getData];
            
        }];

        
        
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.cellModels.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AutoSizeTableViewCell * cells = [[AutoSizeTableViewCell alloc] initWithTableView:tableView];
    
    cells.model = self.cellModels[indexPath.row];
    
    [cells setIconBlock:^{
        NSLog(@"%s",__func__);
        
        UserViewController * userVC = [[UserViewController alloc]init];
        
        [self.superVC.navigationController pushViewController:userVC animated:YES];
        
        
    }];
    
    //点击文本页面
    [cells setTextBlock:^{
        TextUserController * textUser = [[TextUserController alloc]init];
        
        [self.superVC.navigationController pushViewController:textUser animated:YES];
    }];
    return cells;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AutoSizeModel * model = self.cellModels[indexPath.row];
    
    return model.totalHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.selected = NO;
    
}




@end
